package com.sczmgk.hotline_pzh.ui;

import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.widget.Button;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.VerfyCallback;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.dialog.MessageDialog;
import com.sczmgk.hotline_pzh.entity.BaseEntity;
import com.sczmgk.hotline_pzh.entity.IndicatorInfos;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.SecurityCodeUtils;
import com.sczmgk.hotline_pzh.utils.TimerUtils;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.utils.UIUtils;
import com.sczmgk.hotline_pzh.view.RegistItemView;

import java.util.List;

/**
 * Created by Administrator on 2016/12/8 0008.
 */
public class ChangePwdByPhoneActivity extends BaseActivity{

    /*下一步的按钮*/
    private Button mBtnNext;

    /*获取验证码*/
    private RegistItemView mGetKey;

    /*手机号*/
    private RegistItemView mPhone;

    private TimerUtils timerUtils;

    private MessageDialog errorDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);

        //将当前activity添加到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initView();

        setListener();
    }

    /**
     * 添加头
     */
    private void setHeader() {
        setTitle(R.string.change_password);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //手机号
        mPhone = (RegistItemView) findViewById(R.id.riv_change_phone);
        //获取验证码
        mGetKey = (RegistItemView) findViewById(R.id.riv_change_key);
        //下一步
        mBtnNext = (Button) findViewById(R.id.btn_new_phone_next);

        //设置手机号输入类型和描述信息
        mPhone.setInputType(InputType.TYPE_CLASS_PHONE);
        mPhone.setTextDes(R.string.login_phone);
    }

    /**
     * 设置监听
     */
    private void setListener() {

        //获取验证码的点击事件
        mPhone.setOnGetKeyOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                final String phone = mPhone.getText();
                //验证手机号
                boolean isPhone = Tools.isMobilePhoneNumber(phone);
                if (isPhone){
                    //TODO 获取验证码
                    SecurityCodeUtils.getCode(ChangePwdByPhoneActivity.this , phone, 2, 0,new VerfyCallback() {

                        @Override
                        public void onSuccess() {
                            timerUtils = new TimerUtils();
                            timerUtils.startTimer(ChangePwdByPhoneActivity.this , mPhone.getVerifyButton());
                            MyToast.show(ChangePwdByPhoneActivity.this,"验证码已发送至:"+phone);
                        }

                        @Override
                        public void onError(BaseEntity.MyError mError) {
                            if (errorDialog == null) {
                                errorDialog = DialogFactory.warningDialog(ChangePwdByPhoneActivity.this, mError.msg);
                            }else if (!errorDialog.isShowing()){
                                errorDialog = DialogFactory.warningDialog(ChangePwdByPhoneActivity.this, mError.msg);
                            }
                        }

                        @Override
                        public void onLoadIndicatorDataCallback(List<IndicatorInfos> indicatorInfos) {

                        }
                    });
                }else{
                    MyToast.show(ChangePwdByPhoneActivity.this , "请输入正确的手机号");
                }
            }
        }) ;

        //下一步的点击事件
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String phone = mPhone.getText();
                String key = mGetKey.getText();

                if (Tools.isEmpty(phone)){
                    MyToast.show(ChangePwdByPhoneActivity.this , "手机号不能为空");
                    return ;
                }

                if (Tools.isEmpty(key)){
                    MyToast.show(ChangePwdByPhoneActivity.this , "请输入验证码!");
                    return ;
                }

                //验证手机号
                boolean isPhone = Tools.isMobilePhoneNumber(phone);
                if (isPhone) {
                    //提交服务器验证手机号和验证码
                    submitData(phone , key) ;
                }else{
                    MyToast.show(ChangePwdByPhoneActivity.this , "请输入正确的手机号");
                }
            }
        });

    }

    /**
     * 验证验证码是否正确
     * @param phone
     * @param key
     */
    private void submitData(final String phone, String key) {

        loadingDialog = DialogFactory.getLoadingDialog(ChangePwdByPhoneActivity.this);
        DialogFactory.showDialog(loadingDialog);
        SecurityCodeUtils.verfiCode(ChangePwdByPhoneActivity.this, phone, key, new VerfyCallback() {
            @Override
            public void onSuccess() {
                DialogFactory.dismissLoadingDialog(loadingDialog);
                UIUtils.startActivity(ChangePwdByPhoneActivity.this, ChangePwdActivity.class,"phone",phone);

            }

            @Override
            public void onError(BaseEntity.MyError mError) {
                DialogFactory.dismissLoadingDialog(loadingDialog);
                DialogFactory.warningDialog(ChangePwdByPhoneActivity.this,mError.msg);
            }

            @Override
            public void onLoadIndicatorDataCallback(List<IndicatorInfos> indicatorInfos) {

            }
        });
    }

    @Override
    public void handleMessage(Message msg) {

    }

    @Override
    public boolean getIsNeedLogin() {
        return false;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (timerUtils != null && timerUtils.timerIsRunning()){
            timerUtils.stopTimer(mPhone.getVerifyButton());
        }
    }
}
